package cn.iocoder.yudao.module.enterprise.service.controlplan;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.controlplan.vo.ControlPlanCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.controlplan.vo.ControlPlanPageReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.controlplan.vo.ControlPlanUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.controlplan.ControlPlanConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.controlplan.ControlPlanDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.controlplan.ControlPlanMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.CONTROL_PLAN_NOT_EXISTS;

/**
 * 控制计划 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class ControlPlanServiceImpl implements ControlPlanService {

    @Resource
    private ControlPlanMapper controlPlanMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createControlPlan(ControlPlanCreateReqVO createReqVO) {
        // 插入
        ControlPlanDO controlPlan = ControlPlanConvert.INSTANCE.convert(createReqVO);
        controlPlanMapper.insert(controlPlan);
        // 返回
        return controlPlan.getId();
    }

    @Override
    public void updateControlPlan(ControlPlanUpdateReqVO updateReqVO) {
        // 校验存在
        validateControlPlanExists(updateReqVO.getId());
        // 更新
        ControlPlanDO updateObj = ControlPlanConvert.INSTANCE.convert(updateReqVO);
        controlPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteControlPlan(Long id) {
        // 校验存在
        validateControlPlanExists(id);
        // 删除
        controlPlanMapper.deleteById(id);
    }

    private void validateControlPlanExists(Long id) {
        if (controlPlanMapper.selectById(id) == null) {
            throw exception(CONTROL_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public ControlPlanDO getControlPlan(Long id) {
        ControlPlanDO controlPlanDO = controlPlanMapper.selectById(id);
        AdminUserRespDTO user = adminUserApi.getUser(Long.valueOf(controlPlanDO.getUpdater())).getCheckedData();
        controlPlanDO.setUpdater(user.getNickname());
        return controlPlanDO;
    }

    @Override
    public PageResult<ControlPlanDO> getControlPlanPage(ControlPlanPageReqVO pageReqVO) {
        PageResult<ControlPlanDO> page = controlPlanMapper.selectPage(pageReqVO);
        List<ControlPlanDO> list = page.getList();
        // 获取更新者名字
        list = list.stream().map(controlPlanDO -> {
            AdminUserRespDTO user = adminUserApi.getUser(Long.valueOf(controlPlanDO.getUpdater())).getCheckedData();
            controlPlanDO.setUpdater(user.getNickname());
            return controlPlanDO;
        }).collect(Collectors.toList());
        page.setList(list);
        return page;
    }

}
